<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <addTree :tree-data="company" :is-show="true" @addDepartment="addDepartment" />
        <hr>
        <el-tree :data="depats" :props="{label:'name'}" default-expand-all>
          <template #default="{data}">
            <addTree
              :tree-data="data"
              @delDepartments="getDepartments"
              @addDepartment="addDepartment"
              @editDepartment="editDepartment"
            />
          </template>
        </el-tree>
      </el-card>
      <addDept ref="addDept" :show-dialog.sync="showDialog" :node-data="nodeData" @addDepartments="getDepartments" />
    </div>
  </div>
</template>

<script>
import addTree from './components/addTree.vue'
import { departments } from '@/api/departments'
import { listToTreeData } from '@/utils/index'
import addDept from './components/add-dept.vue'
export default {
  components: { addTree, addDept },
  data() {
    return {
      company:
        {
          name: '江苏传智播客教育科技股份有限公司',
          manegess: '负责人',
          id: ''
        },
      depats: [],
      showDialog: false,
      nodeData: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async   getDepartments() {
      const { depts } = await departments()
      this.depats = listToTreeData(depts, '')
    },
    addDepartment(nodeData) {
      this.showDialog = true
      // console.log(nodeData)
      this.nodeData = nodeData
    },
    async  editDepartment(nodeData) {
      await this.$refs.addDept.getDepartmentDetails(nodeData.id)
      this.showDialog = true
      this.nodeData = nodeData
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
